package com.eims.sls_app.ui;

import java.util.Timer;
import java.util.TimerTask;


import com.android.volley.toolbox.RequestParams;
import com.eims.sls_app.dataaccess.CircleBean.CommentBean;
import com.eims.sls_app.dataaccess.utils.HttpUtils;
import com.eims.sls_app.protocol.RspBodyBaseBean;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.BaseHelper;
import com.eims.sls_app.view.MyToast;

import bls.juejin.cn.R;
import progressBar.ProgressDialogBar;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class FupanCommentActivity extends MyBaseActivity implements OnClickListener {
	private static final String TAG = "CommentActivity";
	private ImageView btn_back;
	private TextView tv_Published;
	private EditText et_comment;
	private int id, size, position;
	private MyHandler myHandler;
	private Context context;
	private String comment;
	private String opt, auth, info, time, imei, crc; // 格式化后的参数
	CommentBean commentBean;
	private ProgressDialog pd;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_comment_fudan);
		myHandler = new MyHandler();
		initView();
		initlist();
	}

	private void initlist() {
		btn_back.setOnClickListener(this);
		tv_Published.setOnClickListener(this);

	}

	private void initView() {
		id = getIntent().getIntExtra("id", 0);
		size = getIntent().getIntExtra("size", 1);
		btn_back = (ImageView) findViewById(R.id.btn_back);
		tv_Published = (TextView) findViewById(R.id.tv_Published);
		et_comment = (EditText) findViewById(R.id.et_comment);
		Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			public void run() {
				InputMethodManager inputManager = (InputMethodManager) et_comment.getContext()
						.getSystemService(Context.INPUT_METHOD_SERVICE);
				inputManager.showSoftInput(et_comment, 0);
			}
		}, 500);
		/*
		 * et_comment.setFocusable(true); et_comment.setFocusableInTouchMode(true);
		 * et_comment.requestFocus(); InputMethodManager inputManager =
		 * (InputMethodManager)et_comment.getContext().getSystemService(Context.
		 * INPUT_METHOD_SERVICE); inputManager.showSoftInput(et_comment, 0);
		 */
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btn_back:
			ProgressDialogBar.startProgress(FupanCommentActivity.this, "sff", false);
			myHandler.sendEmptyMessageDelayed(4,100);
			break;
		case R.id.tv_Published:
			comment = et_comment.getText().toString().trim();
			if (comment.equals("")) {
				MyToast.getToast(this, "评论不能为空").show();
			} else {

				pd = BaseHelper.showProgress(FupanCommentActivity.this, "", "加载中...", true, false);
				MyAsynTask myAsynTask = new MyAsynTask();
				myAsynTask.execute();
			}

			break;

		default:
			break;
		}
	}

	class MyAsynTask extends AsyncTask<Integer, Integer, String> {

		String error = "-1";

		@Override
		protected String doInBackground(Integer... params) {

			opt = "104";
			time = RspBodyBaseBean.getTime();
			imei = RspBodyBaseBean.getIMEI(FupanCommentActivity.this);

			info = RspBodyBaseBean.change_comment(id + "", comment);

			// String key = MD5.md5(App.user.getUserPass() + App.MD5_key);
			String key = App.key;
			crc = RspBodyBaseBean.getCrc(time, imei, key, info, App.user.getUid());
			auth = RspBodyBaseBean.getAuth(crc, time, imei, App.user.getUid());
			String[] values = { opt, auth, info };
			Log.i(TAG, "评论" + RequestParams.convertParams(FupanCommentActivity.this, opt, info));
			String result = HttpUtils.doPost(App.names, values, App.path);
			System.out.println("评论：" + result);
			if (!result.equals("")) {
				if (result.equals("401")) {
					error = "1";
				} else {
                      if (result.equals("success")) {
                    	  error = "0";
					}else {
						 error = "-1";
					}
					
					
				}

			}

			return error;
		}

		@Override
		protected void onPostExecute(String result) {

			myHandler.sendEmptyMessage(Integer.parseInt(result));

			super.onPostExecute(result);
		}
	}

	@SuppressLint("HandlerLeak")
	class MyHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			if (pd != null) {
				pd.dismiss();
			}
			switch (msg.what) {
			case 0:
				// MyToast.getToast(context, "评论成功，请重新刷新").show();

//				if (commentBean != null) {
				ProgressDialogBar.startProgress(FupanCommentActivity.this, "sff", false);
					Toast.makeText(FupanCommentActivity.this, "评论成功", Toast.LENGTH_SHORT).show();
					SharedPreferences myAhare = getSharedPreferences("fupan", Activity.MODE_PRIVATE);
					// 实例化SharedPreferences.Editor对象（第二步）
					SharedPreferences.Editor editor = myAhare.edit();
					// 用putString的方法保存数据
					editor.putInt("isfr", 1);
				

					editor.putString("comment", comment);
			

					// 提交当前数据
					editor.commit();
					myHandler.sendEmptyMessageDelayed(3, 1000);
//				} else {
//					Toast.makeText(FupanCommentActivity.this, "评论失败！", Toast.LENGTH_SHORT).show();
//				}

				break;
			case 1:
				// MyToast.getToast(context, "重复晒单").show();
				Toast.makeText(FupanCommentActivity.this, "您未跟单此单 没评论权限", Toast.LENGTH_SHORT).show();
				;
				break;
			case -1:
				Toast.makeText(FupanCommentActivity.this, "评论失败！", Toast.LENGTH_SHORT).show();
				break;
			case 3:
				FupanCommentActivity.this.finish();
				break;
			case 4:
				ProgressDialogBar.stopProgress();
				FupanCommentActivity.this.finish();
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}
	}

}
